﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Services.Client;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NCT.INVPOS.PresentationLayer.NCTWebContext;

namespace NCT.INVPOS.PresentationLayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                NCTWebContext.AppContext context = new NCTWebContext.AppContext(new Uri("http://localhost:13069/NCTWCFService.svc"));

                Product pro = new Product()
                {
                    Name = "asdaaaaa",
                    Code = "asd",
                    Description = "asd",
                    Branch = new Branch()
                    {
                        Name = "GUAGUA"
                    }
                };

                context.AddToBranches(pro.Branch);
                
                context.AddToProducts(pro);

                context.SetLink(pro, "Branch", pro.Branch);

                DataServiceResponse response = context.SaveChanges();

                // Enumerate the returned responses.
                foreach (ChangeOperationResponse change in response)
                {
                    // Get the descriptor for the entity.
                    EntityDescriptor descriptor = change.Descriptor as EntityDescriptor;

                    if (descriptor != null)
                    {
                        Product addedProduct = descriptor.Entity as Product;

                        if (addedProduct != null)
                        {
                            MessageBox.Show(string.Format("New product added with ID {0}.",
                                addedProduct.Id));
                        }
                    }
                }
            }
            catch (DataServiceRequestException ex)
            {
                MessageBox.Show(string.Format("An error occurred when saving changes.", ex));
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
